<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item label="选择机构" prop="institutionNo">
              <el-select v-model="formFilter.institutionNo" style="width:200px">
                <el-option 
                  v-for="item in InstitutionList"
                  :key="item.id"
                  :label="item.institutionName"
                  :value="item.institutionNo"
                ></el-option>
              </el-select>
            </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
    </vue-header>
    <vue-body>
      <el-table
        v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="institutionName"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="institutionPhone"
          label="电话">
        </el-table-column>
        <el-table-column
          label="状态">
          <template scope="scope">
            {{ scope.row.status | state }}
          </template>
        </el-table-column>
        <el-table-column
          prop="institutionColour"
          label="颜色">
          <template scope="scope">
          		<div :style="styleStr(scope.row.institutionColour)"></div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button-group>
              <el-button
              size="small"
              type="primary"
              @click="handleBusiness(scope.$index, scope.row, tableData)">交易信息设置</el-button>
              </el-button-group>
          </template>
      </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
  </vue-content>
</template>

<script>
  import { queryInstitutionList} from "@/services/index";
  import {query, del,forbidden, queryMechanism, use} from '@/services/institution'
  import AuthStorage from '@/services/storage.js'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  export default {
    name: 'role-table',
    components: {},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
        institutionNo: ""
      },
      loading: false,
      view: '',
      InstitutionList:[]
    }),
    methods: {
      async loadData () {
        this.loading = true
        try {
          let data = await query(Object.assign({currentPage: this.currentPage},{perPage:10}, this.formFilter))
          // debugger
          this.tableData = data.data
					this.pageSize = parseInt(data.totalResult)
//					this.pageSize = 1
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async loadCategory () {
        try {
          let data = await queryInstitutionList();
          this.InstitutionList = data
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      imgSrc(data){
      	return this.$store.state.imgUrl + data;
      },
      styleStr(data){
      	return "width: 20px;height: 20px;border: 1px solid #aaa;margin-left:10px; border-radius: 5px;background: #"+data+";"
      },
      async handleBusiness (index, data) {
				this.$router.push({
	        path: 'business/insInfo', 
	        name: 'insInfo',
	        query: { 
            institutionNo: data.institutionNo
          }
        })
      },
      SearchSubmit () {
        this.currentPage = 1
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      editFinish (needRefresh) {
        this.view = ''
        if(needRefresh){
          this.$nextTick(() => {
            // dom元素更新后执行， 因此此处能正确打印出更改之后的值；
            this.loadData()
         	})
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      state (value) {
        return {"01": '启用', "02": '禁用'}[value]
      },
      isIntegral(value){
        return {"0": '不允许', "1": '允许'}[value]
      }
    },
    created () {
      if(AuthStorage.userType == '03'){
          this.$router.push({
            path: 'business/insInfo', 
            name: 'insInfo',
            query: { 
            }
          })
      }else{
        this.loadData();
        this.loadCategory();
      }
    }
  }
</script>

<style>
	.logoImgPic{
		height: 100px;
		display: block;
	}
	.el-table th>.cell{
		text-align: center;
	}
</style>
